###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# Libraries/StaticPages.pm.pl -> StaticPages library                          #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package StaticPages;

BEGIN { require "System.pm.pl";  import System  qw($SYSTEM ); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }

sub new {
  my $class = shift;
  my $self  = { "DB" => undef, @_ };
  return bless ($self, $class);
}

sub DESTROY {}

###############################################################################
# Update subroutine
sub Update {
  my $self = shift;
  my %in = ("TYPE" => "", "INPUT" => {}, "CHILDREN" => 0, @_);
  
  if ($in{'TYPE'} eq "Detail") {
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$in{'INPUT'}->{'ID'}.html" || -e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$in{'INPUT'}->{'ID'}.shtml") {
      open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$in{'INPUT'}->{'ID'}.del") || ($self->{'ERROR'} = $! and return);
      close(FILE);
    }
    
    my $Category = $self->{'DB'}->BinarySelect(
      TABLE   => "Categories",
      KEY     => $in{'INPUT'}->{'CATEGORY'}
    );
    
    $self->Update(TYPE => "Category", INPUT => $Category);
    $self->Update(TYPE => "Index");
    $self->Update(TYPE => "Cool");
    $self->Update(TYPE => "Hot");
    $self->Update(TYPE => "New");
  } elsif ($in{'TYPE'} eq "Category") {
    my @Categories = split(/,/, $in{'INPUT'}->{'ROOT'});
    shift(@Categories);
    push(@Categories, $in{'INPUT'}->{'ID'});
    
    my $Stories;
    if ($in{'CHILDREN'}) {
      my ($subcategories, undef) = $self->{'DB'}->Query(
        TABLE   => "Categories",
        WHERE   => {
          ROOT  => [$in{'INPUT'}->{'ROOT'}.",".$in{'INPUT'}->{'ID'}]
        },
        MATCH   => "ALL"
      );
      
      my @subcategories = &Standard::MapArray("\${ID}", $subcategories);
      
      push(@Categories, @subcategories);
      
      ($Stories, undef) = $self->{'DB'}->Query(
        TABLE   => "Resources",
        WHERE   => {
          CATEGORY  => \@subcategories
        },
        MATCH   => "ANY"
      );
    }
    
    opendir(CATEGORIES, "$SYSTEM->{'PUBLIC_PATH'}/Static/Category") || ($self->{'ERROR'} = $! and return);
    my @Files = readdir(CATEGORIES);
    close(CATEGORIES);
    
    foreach my $category (@Categories) {
      foreach my $file (grep(/^$category\-(.*?)\.(s)?html$/, @Files)) {
        $file =~ m/^$category\-(.*?)\.(s)?html$/;
        open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Category/$category-$1.del") || ($self->{'ERROR'} = $! and return);
        close(FILE);
      }
    }
    
    if ($in{'CHILDREN'}) {
      foreach my $story (@{ $Stories }) {
        next unless (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$story->{'ID'}.html" || -e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$story->{'ID'}.shtml");
        open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$story->{'ID'}.del") || ($self->{'ERROR'} = $! and return);
        close(FILE);
      }

      $self->Update(TYPE => "Index");
      $self->Update(TYPE => "Cool");
      $self->Update(TYPE => "Hot");
      $self->Update(TYPE => "New");
    }
  } elsif ($in{'TYPE'} eq "Index") {
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Index.html" || -e "$SYSTEM->{'PUBLIC_PATH'}/Static/Index.shtml") {
      open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Index.del") || ($self->{'ERROR'} = $! and return);
      close(FILE);
    }
  } elsif ($in{'TYPE'} eq "Cool") {
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.html" || -e "$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.shtml") {
      open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.del") || ($self->{'ERROR'} = $! and return);
      close(FILE);
    }
  } elsif ($in{'TYPE'} eq "Hot") {
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.html" || -e "$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.shtml") {
      open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.del") || ($self->{'ERROR'} = $! and return);
      close(FILE);
    }
  } elsif ($in{'TYPE'} eq "New") {
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/New.html" || -e "$SYSTEM->{'PUBLIC_PATH'}/Static/New.shtml") {
      open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/New.del") || ($self->{'ERROR'} = $! and return);
      close(FILE);
    }
  }

  return 1; 
}

###############################################################################
# Remove subroutine
sub Remove {
  my $self = shift;
  my %in = ("TYPE" => "", "KEYS" => [], @_);

  if ($in{'TYPE'} eq "Detail") {
    foreach my $key (@{ $in{'KEYS'} }) {
      if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$key.shtml") {
        unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$key.shtml") || ($self->{'ERROR'} = $! and return);
      }
      if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$key.html") {
        unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$key.html") || ($self->{'ERROR'} = $! and return);
      }
      if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$key.del") {
        unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$key.del") || ($self->{'ERROR'} = $! and return);
      }
    }
  } elsif ($in{'TYPE'} eq "Category") {
    opendir(CATEGORIES, "$SYSTEM->{'PUBLIC_PATH'}/Static/Category") || ($self->{'ERROR'} = $! and return);
    my @Files = readdir(CATEGORIES);
    close(CATEGORIES);
    
    foreach my $key (@{ $in{'KEYS'} }) {
      foreach my $file (grep(/^$key\-(.*)$/, @Files)) {
        unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Category/$file") || ($self->{'ERROR'} = $! and return);
      }
    }
  } elsif ($in{'TYPE'} eq "Index") {
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Index.shtml") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Index.shtml") || ($self->{'ERROR'} = $! and return);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Index.html") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Index.html") || ($self->{'ERROR'} = $! and return);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Index.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Index.del") || ($self->{'ERROR'} = $! and return);
    }
  } elsif ($in{'TYPE'} eq "Cool") {
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Cool.shtml") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Cool.shtml") || ($self->{'ERROR'} = $! and return);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Cool.html") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Cool.html") || ($self->{'ERROR'} = $! and return);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Cool.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Cool.del") || ($self->{'ERROR'} = $! and return);
    }
  } elsif ($in{'TYPE'} eq "Hot") {
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Hot.shtml") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Hot.shtml") || ($self->{'ERROR'} = $! and return);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Hot.html") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Hot.html") || ($self->{'ERROR'} = $! and return);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Hot.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/Hot.del") || ($self->{'ERROR'} = $! and return);
    }
  } elsif ($in{'TYPE'} eq "New") {
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/New.shtml") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/New.shtml") || ($self->{'ERROR'} = $! and return);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/New.html") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/New.html") || ($self->{'ERROR'} = $! and return);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/New.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/New.del") || ($self->{'ERROR'} = $! and return);
    }
  } elsif ($in{'TYPE'} eq "StyleSheet") {
    foreach my $key (@{ $in{'KEYS'} }) {
      if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/CSS/$key.css") {
        unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/CSS/$key.css") || ($self->{'ERROR'} = $! and return);
      }
    }
  }
  
  return 1;
}

###############################################################################
# Build subroutine
sub Build {
  my $self = shift;
  my %in = ("TYPE" => "", "ID" => "", "PAGE" => "", @_);
  
  if ($in{'TYPE'} eq "Detail") {
    require "Detail.pm.pl";
    my $Source = Detail->new();
    
    open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$in{'ID'}.".($SYSTEM->{'SSI'} ? "shtml" : "html")) || ($self->{'ERROR'} = $! and return);
    print FILE $Source->show(DB => $self->{'DB'}, STATIC => 1);
    close(FILE);
    
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$in{'ID'}.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$in{'ID'}.del") || ($self->{'ERROR'} = $! and return);
    }
  } elsif ($in{'TYPE'} eq "Category") {
    require "Category.pm.pl";
    my $Source = Category->new();
    
    $SL::QUERY{'Sort'}    = $GENERAL->{'CATEGORY_DEFAULT_SORT'};
    $SL::QUERY{'By'}      = $GENERAL->{'CATEGORY_DEFAULT_BY'};
    $SL::QUERY{'Page'}    = $in{'PAGE'};
    $SL::QUERY{'PerPage'} = $GENERAL->{'CATEGORY_DEFAULT_PERPAGE'};
    
    open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Category/$in{'ID'}-$in{'PAGE'}.".($SYSTEM->{'SSI'} ? "shtml" : "html")) || ($self->{'ERROR'} = $! and return);
    print FILE $Source->show(DB => $self->{'DB'}, STATIC => 1);
    close(FILE);

    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Category/$in{'ID'}-$in{'PAGE'}.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Category/$in{'ID'}-$in{'PAGE'}.del") || ($self->{'ERROR'} = $! and return);
    }
  } elsif ($in{'TYPE'} eq "Index") {
    require "Index.pm.pl";
    my $Source = Index->new();
    
    open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Index.".($SYSTEM->{'SSI'} ? "shtml" : "html")) || ($self->{'ERROR'} = $! and return);
    print FILE $Source->show(DB => $self->{'DB'}, STATIC => 1);
    close(FILE);

    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Index.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Index.del") || ($self->{'ERROR'} = $! and return);
    }
  } elsif ($in{'TYPE'} eq "Cool") {
    require "Cool.pm.pl";
    my $Source = Cool->new();
    
    open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.".($SYSTEM->{'SSI'} ? "shtml" : "html")) || ($self->{'ERROR'} = $! and return);
    print FILE $Source->show(DB => $self->{'DB'}, STATIC => 1);
    close(FILE);

    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.del") || ($self->{'ERROR'} = $! and return);
    }
  } elsif ($in{'TYPE'} eq "Hot") {
    require "Hot.pm.pl";
    my $Source = Hot->new();
    
    open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.".($SYSTEM->{'SSI'} ? "shtml" : "html")) || ($self->{'ERROR'} = $! and return);
    print FILE $Source->show(DB => $self->{'DB'}, STATIC => 1);
    close(FILE);

    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.del") || ($self->{'ERROR'} = $! and return);
    }
  } elsif ($in{'TYPE'} eq "New") {
    require "New.pm.pl";
    my $Source = New->new();
    
    open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/New.".($SYSTEM->{'SSI'} ? "shtml" : "html")) || ($self->{'ERROR'} = $! and return);
    print FILE $Source->show(DB => $self->{'DB'}, STATIC => 1);
    close(FILE);

    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/New.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/New.del") || ($self->{'ERROR'} = $! and return);
    }
  }

  return 1;
}

###############################################################################
# BuildStyleSheet subroutine
sub BuildStyleSheet {
  my $self = shift;
  my ($StyleSheet) = @_;

  return unless ($StyleSheet);

  $SL::QUERY{'StyleSheet'} = $StyleSheet;

  require "StyleSheet.pm.pl";
  my $Source = StyleSheet->new();
  
  open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/CSS/$StyleSheet.css") || ($self->{'ERROR'} = $! and return);
  print FILE $Source->show(DB => $self->{'DB'}, STATIC => 1);
  close(FILE);
  
  return 1;
}

1;